/*
 * Copyright (c) 2019-2020, https://api.iot.10086.cn/(https://gitee.com/cmcc-iot-api/iot-card-open-application/).
 */
package cn.iot.card.open.sim.controller;

import cn.iot.card.open.constants.Constants;
import cn.iot.card.open.context.LoginContextHolder;
import cn.iot.card.open.context.model.LoginUser;
import cn.iot.card.open.exception.enums.BizExceptionEnum;
import cn.iot.card.open.modular.entity.AccountCard;
import cn.iot.card.open.modular.entity.Card;
import cn.iot.card.open.modular.entity.UserGroupRl;
import cn.iot.card.open.modular.log.ApiBussinessLog;
import cn.iot.card.open.modular.service.AccountCardService;
import cn.iot.card.open.modular.service.CardService;
import cn.iot.card.open.modular.service.UserService;
import cn.iot.card.open.pojo.page.LayuiPageFactory;
import cn.iot.card.open.pojo.page.LayuiPageInfo;
import cn.iot.card.open.sim.dto.general.SimAccountDTO;
import cn.iot.card.open.sim.service.GeneralRechargeService;
import cn.iot.card.open.util.CommonUtil;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 物联卡综合信息-充值控制器
 *
 * @author 何光鑫
 * @date 20200326 09:18:42
 * @modify 20200326 何光鑫 v1.1.2.1 创建
 * @since v1.1.2.1
 */
@Controller
@RequestMapping("/generalRecharge")
public class GeneralReChargeController{

    /**
     * 页面路径前缀
     */
    private static String PREFIX = "/modular/sim/general/recharge/";

    @Autowired
    private GeneralRechargeService generalRechargeService;

    @Autowired
    private CardService cardService;

    @Autowired
    private UserService userService;

    @Autowired
    private AccountCardService accountCardService;

    /**
     * 充值结果查询
     *
     * @param accountId 集团id
     * @param queryWay 查询方式（充值时间查询、充值订单号查询）
     * @param orderNo 充值订单号
     * @param startTime 查询开始时间
     * @param endTime 查询结束时间
     * @return
     * @throws IOException
     */
    @RequestMapping("/queryRechargeResult")
    @ResponseBody
    public LayuiPageInfo queryRechargeResult(@RequestParam(required = false) String accountId, String queryWay, @RequestParam(required = false) String orderNo,
                                             @RequestParam(required = false) String startTime,@RequestParam(required = false) String endTime) throws IOException {
        if (ToolUtil.isEmpty(accountId)) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.ACCOUNT_NOT_FOUND);
        }
        String transId = CommonUtil.generateTransId(accountId);
        Map<String, String> parameterMap = new HashMap<>();
        if(queryWay.equals(Constants.QUERY_WAY_BY_ORDER_NO)){
            parameterMap.put(Constants.RESPONSE_ORDER_NO,orderNo);
        }else{
            parameterMap.put(Constants.RESPONSE_START_TIME,startTime);
            parameterMap.put(Constants.RESPONSE_END_TIME,endTime);
        }
        return generalRechargeService.queryRechargeResult(transId,accountId,parameterMap);
    }

    /**
     * 跳转到物联卡充值交费页面
     *
     * @return 返回 string
     * @author 何光鑫
     * @date 20200326 10:57:18
     * @modify 20200326 何光鑫 v1.1.2.1 创建
     * @since v1.1.2.1
     */
    @RequestMapping("/toRechargeSimAccount")
    public String toRechargeSimAccount() {
        return PREFIX + "rechargeSimAccount.html";
    }


    /**
     * 物联卡充值交费
     *
     * @param simAccountDTO 物联卡充值缴费请求参数
     * @return 返回 response data
     * @throws Exception
     * @author 何光鑫
     * @date 20200326 10:57:14
     * @modify 20200326 何光鑫 v1.1.2.1 创建
     * @since v1.1.2.1
     */
    @RequestMapping("/rechargeSimAccount")
    @ApiBussinessLog(value = "物联卡账户充值交费")
    @ResponseBody
    public ResponseData rechargeSimAccount(SimAccountDTO simAccountDTO) throws Exception {
        //充值标识类型为msisdn
        String accountId = simAccountDTO.getAccountId();
        String entityType = simAccountDTO.getEntityType();
        String entityId = simAccountDTO.getEntityId();
        if(entityType.equals(Constants.RESPONSE_ENTITY_TYPE_MSISDN)) {
            Card card = cardService.findCard(Constants.REQUEST_PARAM_MSISDN, entityId);
            if (null == card) {
                throw new ServiceException(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
            }
            LoginUser loginUser = LoginContextHolder.getContext().getUser();
            UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
            if (null == userGroupRl) {
                throw new ServiceException(BizExceptionEnum.CARD_NOT_BELONG_USER);
            }

            AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
            accountId = String.valueOf(accountCard.getAccountId());
        }
        String transId = CommonUtil.generateTransId(accountId);
        String chargeMoney = simAccountDTO.getChargeMoney();
        String paymentType = simAccountDTO.getPaymentType();
        String returnUrl = simAccountDTO.getReturnUrl();
        String defaultBank = simAccountDTO.getDefaultBank();
        return generalRechargeService.rechargeSimAccount(transId, accountId, entityType, entityId, chargeMoney, paymentType, returnUrl, defaultBank);
    }
}
